/**
 Ceated by Amit Mudgal (MicroStrategy Principal Designer) for GMS on 6/20/2014
 *
 */
package com.amex.uam.tasks;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

public class MailSender {
	
	public static String MAIL_HOST ="lpqiu523.trcw.us.aexp.com";
	
	public static String AEXP_MAIL_SENDER = "Mail Sender";
	
	public static enum RPA {
		MARC, ICARD, DASHBOARD, ALL
	};
	
	/**
	 * 	
	 	Email will be sent to below Group ID based on Requested Product Access.
		
	 	1. If only MARC product Requested- 
		From: Microstrategy_User_Administration@aexp.com
		to: Manager Email Add
		CC: gmar.customer.support@aexp.com, Users Email Add
	
		2. 1. If only ICARD/DASHBOARD product Requested- 
		From: Microstrategy_User_Administration@aexp.com
		to: Manager Email Add
		CC: Mystic.customer.support@aexp.com, Users Email Add
	
		3. 1. If both ICARD/DASHBOARD and MARC product Requested- 
		From: Microstrategy_User_Administration@aexp.com
		to: Manager Email Add
		CC: gmar.customer.support@aexp.com,Mystic.customer.support@aexp.com, Users Email Add

	 * 	 
	 * @param type
	 * @param mgrMail comma separated address strings
	 * @param usrMail comma separated address strings
	 * @param subject
	 * @param content
	 * @return
	 */
	public static MailBean createRPAMailBean(RPA type, String mgrMail, String usrMail, String subject, String content) {
		
		MailBean bean = new MailBean();
		
		switch(type) {
		case MARC:
			bean.setFrom("Microstrategy_User_Administration@aexp.com");
			bean.setTo(mgrMail);
			bean.setCc("gmar.customer.support@aexp.com,"+usrMail);
			break;
		case ICARD:
		case DASHBOARD:
			bean.setFrom("Microstrategy_User_Administration@aexp.com");
			bean.setTo(mgrMail);
			bean.setCc("Mystic.customer.support@aexp.com," + usrMail);
			break;
		case ALL:
			bean.setFrom("Microstrategy_User_Administration@aexp.com");
			bean.setTo(mgrMail);
			bean.setCc("gmar.customer.support@aexp.com,Mystic.customer.support@aexp.com," + usrMail);
			break;
		default:
			// 
		}
		
		bean.setMailhost(MAIL_HOST);
		bean.setMailer(AEXP_MAIL_SENDER);
		bean.setSubject(subject);
		bean.setContent(content);
		
		return bean;
	}
	
	public static boolean send(MailBean bean) {
		try {
			
			//Properties props = System.getProperties();
			
			// XXX - could use Session.getTransport() and Transport.connect()
			// XXX - assume we're using SMTP
			/*if (bean.getMailhost() != null)
				props.put("mail.smtp.host", bean.getMailhost());*/
			
			// Get a Session object
			//String mstrMailServer_smtp_host = mailServerProps.getProperty("mail.smtp.host");
			
	        Properties props = new Properties();
	        props.put("mail.transport.protocol", "smtp");
	        props.setProperty("mail.smtps.starttls.enable", "true");
	        props.put("mail.smtp.host", bean.getMailhost());
	        props.put("mail.smtp.port", "25");
	        props.put("mail.from", bean.getFrom());
	        props.put("mail.debug", "true");
	        props.put("mail.smtp.socketFactory.fallback", "false");
	        props.setProperty("mail.smtp.quitwait", "false");
	        Session session = Session.getInstance(props, null);
	        session.setDebug(true);
		

			// construct the message
			Message msg = new MimeMessage(session);
			
			if (bean.getFrom() != null)
				msg.setFrom(new InternetAddress(bean.getFrom()));
			else
				msg.setFrom();

			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(bean.getTo(), false));
			if (bean.getCc() != null)
				msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(bean.getCc(), false));
			if (bean.getBcc() != null)
				msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bean.getBcc(), false));

			msg.setSubject(bean.getSubject());
			msg.setSentDate(new Date());
			collect(bean.getContent(), msg);

			msg.setHeader("X-Mailer", bean.getMailer());
			msg.setSentDate(new Date());

			// send the thing off
			Transport.send(msg);

			return true;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	private static void collect(String text, Message msg)
			throws MessagingException, IOException {
		String subject = msg.getSubject();
		StringBuffer sb = new StringBuffer();
		sb.append("<HTML>\n");
		sb.append("<HEAD>\n");
		sb.append("<TITLE>\n");
		sb.append(subject + "\n");
		sb.append("</TITLE>\n");
		sb.append("</HEAD>\n");

		sb.append("<BODY>\n");
		sb.append("<H1>" + subject + "</H1>" + "\n");

		sb.append(text);

		sb.append("</BODY>\n");
		sb.append("</HTML>\n");

		msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html")));
	}
	
	public static void main(String[] args) {
		
		MailBean bean = new MailBean();
		bean.setFrom("benjamin.qi@aexp.com");
		bean.setTo("kyle.zhu@aexp.com");
		bean.setCc(null);
		bean.setBcc(null);
		bean.setMailhost("lpqiu523.trcw.us.aexp.com");
		//bean.setMailhost("outlook.024d.mgd.msft.net");
		bean.setSubject("test mail");
		//bean.setMailer("Microsoft Outlook Express 6.00.2900.5931");
		bean.setMailer("Microsoft Outlook Express 6.00.2900.5931");
		bean.setContent("Test mail content");
		
		boolean success = MailSender.send(bean);
		System.out.println(success ? "Mail was sent successfully." : "Mail was sent failed.");
	}
	
}
